package com.ruoyi.business.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusDevicePressure;
import com.ruoyi.business.service.IBusDevicePressureService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 调压设施台账Controller
 * 
 * @author wf
 * @date 2024-07-17
 */
@RestController
@RequestMapping("/business/pressure")
public class BusDevicePressureController extends BaseController
{
    @Autowired
    private IBusDevicePressureService busDevicePressureService;
    @Value("${export.max:10000}")
    private Integer max;

    /**
     * 查询调压设施台账列表
     */
    @PreAuthorize("@ss.hasPermi('business:pressure:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusDevicePressure busDevicePressure)
    {
        startPage();
        List<BusDevicePressure> list = busDevicePressureService.selectBusDevicePressureList(busDevicePressure);
        return getDataTable(list);
    }

    /**
     * 导出调压设施台账列表
     */
    @PreAuthorize("@ss.hasPermi('business:pressure:export')")
    @Log(title = "调压设施台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusDevicePressure busDevicePressure)
    {
        List<BusDevicePressure> list = busDevicePressureService.selectBusDevicePressureList(busDevicePressure);
        if (list.size() > max) {
            throw new ServiceException("导出失败！,每次最多导出 " + max + "条");
        }
        ExcelUtil<BusDevicePressure> util = new ExcelUtil<BusDevicePressure>(BusDevicePressure.class);
        util.exportExcel(response, list, "调压设施台账数据");
    }

    /**
     * 获取调压设施台账详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:pressure:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(busDevicePressureService.selectBusDevicePressureById(id));
    }

    /**
     * 新增调压设施台账
     */

    @PreAuthorize("@ss.hasPermi('business:pressure:add')")
    @Log(title = "调压设施台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusDevicePressure busDevicePressure)
    {

//        根据新增的设施的type字段判断新增的是哪种类型的设施，并更新对应的数量字段。
//        使用监听器 or aop ？

//        System.out.println("==============================================" + busDevicePressure.toString());
        return toAjax(busDevicePressureService.insertBusDevicePressure(busDevicePressure));
    }

    /**
     * 修改调压设施台账
     */
    @PreAuthorize("@ss.hasPermi('business:pressure:edit')")
    @Log(title = "调压设施台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusDevicePressure busDevicePressure)
    {
        logger.info("busDevicePressure:" + busDevicePressure.toString());
        return toAjax(busDevicePressureService.updateBusDevicePressure(busDevicePressure));
    }

    /**
     * 删除调压设施台账
     */
    @Transactional
    @PreAuthorize("@ss.hasPermi('business:pressure:remove')")
    @Log(title = "调压设施台账", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}/{nhids}")
    public AjaxResult remove(@PathVariable Long[] ids,@PathVariable Long[] nhids)
    {
//        System.out.println("nhids:" + Arrays.toString(nhids));
//        根据删除设施时，根据其对应的type，更新对应的数量字段。
        return toAjax(busDevicePressureService.deleteBusDevicePressureByIds(ids));
    }

    @Transactional
    @PreAuthorize("@ss.hasPermi('business:pressure:remove')")
    @Log(title = "调压设施台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/single/{ids}")
    public AjaxResult singleremove(@PathVariable Long[] ids)
    {
//        System.out.println("nhids:" + Arrays.toString(nhids));
//        根据删除设施时，根据其对应的type，更新对应的数量字段。
        return toAjax(busDevicePressureService.deleteBusDevicePressureByIds(ids));
    }


}
